<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>KiweeCommerce | Installer</title>
	<link rel="stylesheet" href="install_files/css/installer.css" type="text/css" charset="utf-8">
	</head>
<body>
<div id="container">
  <div id="header"><img src="install_files/images/KiweeCommerceWonB_18.gif" alt="KiweeCommerce" width="261" height="65" /></div>
  <div class="bar"></div>
  <div id="main">

  <h1>Welcome to the KiweeCommerce installation wizard.</h1>
  <p>This wizard will guide you through the process of installing your e-Commerce module.</p>
  <p>Please note that the fields below have been auto-populated from your ModX configuration file config.inc.php</p>
  <p>Please Make sure that the following conditions have been met</p>
  <ul>
	<li>
	 	The &quot;Base Path&quot; below is the root of your ModX site <br />
	    <strong>Note: </strong>the same folder as assets and manager
	</li>
	<br />
	<li>
		The database information that was pre-populated appears correct.<br />
	    <strong>Note: </strong> if the password field is filled, it is most likely correct as it was pulled from your configuration file
	</li>
	<br />
	<li>
		The Table Prefix is the same table prefix you use in your modx installation.<br />
	    <strong>Note: </strong>KiweeCommerce tables will have two prefixes, <em>ie: modx_kiwee_order_header</em>
	</li>
  </ul>
  <br />
  <p>Please click the &quot;Install Now&quot; button to perform the KiweeCommerce installation procedure.</p>
  <div id="kiwee"></div>
	
<form method="post" action="index.php">
<?php
	$installok = 0;
	if (isset($_POST["install"]) && $_POST["install"] == 1) {
		$database_server = $_POST["database_server"];
	    $database_user = $_POST["database_user"];
	    $database_password = $_POST["database_password"];
	    $dbase = $_POST["database_db"];
	    $table_prefix = $_POST["table_prefix"];
	    $adminname = '';
	    $adminemail = '';
	    $adminpass = '';
	    $basepath = $_POST["path_base"];
	    $snippath = 'assets/snippets/';
	    $mngrpath = 'manager/';
		
		$installok = 1;
		
		// Sanity checks
		$conn = @mysql_connect($database_server, $database_user, $database_password);
		if ($conn === FALSE) {
			echo '<h2><font color="#FF0000">Unable to connect to database ' . $database_server . ' as ' . $database_user .
			  	':' . $database_password . ': ' . mysql_error() . '</font></h2>';
			$installok = 0;
		}
		
		if ($conn !== FALSE && mysql_select_db($dbase, $conn) === FALSE) {
			echo '<h2><font color="#FF0000">Unable to select database ' . $dbase . ': ' . mysql_error() . '</font></h2>';
			$installok = 0;
		}
		@mysql_close($conn);

		if (substr($basepath, -1, 1) != '/') {
			echo '<h2><font color="#FF0000">Base path requires a trailing slash</font></h2>';
			$installok = 0;
		}
		
		if (substr($mngrpath, -1, 1) != '/') {
			echo '<h2><font color="#FF0000">Manager path requires a trailing slash</font></h2>';
			$installok = 0;
		}
		
		if (substr($snippath, -1, 1) != '/') {
			echo '<h2><font color="#FF0000">Snippets path requires a trailing slash</font></h2>';
			$installok = 0;
		}
		
		if (substr($basepath, 0, 1) != '/' && substr($basepath, 1, 1) != ':') {
			echo '<h2><font color="#FF0000">Base path requires a leading slash (absolute path)</font></h2>';
			$installok = 0;
		}
		
		if (substr($mngrpath, 0, 1) == '/') {
			echo '<h2><font color="#FF0000">Manager path must not have a leading slash</font></h2>';
			$installok = 0;
		}
		
		if (substr($snippath, 0, 1) == '/') {
			echo '<h2><font color="#FF0000">Snippets path must not have a leading slash</font></h2>';
			$installok = 0;
		}
		
		if (!file_exists($basepath . 'index.php')) {
			echo '<h2><font color="#FF0000">MODx not found in ' . $basepath . '</font></h2>';
			$installok = 0;
		}

		if (!file_exists($basepath . $mngrpath . 'includes')) {
			echo '<h2><font color="#FF0000">Includes directory not found in Manager path ' . $basepath . $mngrpath . '</font></h2>';
			$installok = 0;
		}

		if (!file_exists($basepath . $snippath)) {
			echo '<h2><font color="#FF0000">Snippets path ' . $basepath . $snippath . ' not valid</font></h2>';
			$installok = 0;
		}
		
		if (touch($basepath . $mngrpath . 'index.php') === FALSE) {
			echo '<h2><font color="#FF0000">Unable to write to index.php to install menus - check permissions</font></h2>';
			$installok = 0;
		}
		
		if (touch($basepath . $mngrpath . 'kiwee_testperms') === FALSE) {
			echo '<h2><font color="#FF0000">Unable to write to manager subdirectory - check permissions</font></h2>';
			$installok = 0;
		} else unlink($basepath . $mngrpath . 'kiwee_testperms');
		
		if (touch($basepath . $snippath . 'kiwee_testperms') === FALSE) {
			echo '<h2><font color="#FF0000">Unable to write to snippets subdirectory - check permissions</font></h2>';
			$installok = 0;
		} else unlink($basepath . $snippath . 'kiwee_testperms');
	} else {
		// Set some defaults
		include_once('../manager/includes/config.inc.php');
		$dbase = substr($dbase,1, strlen($dbase));
		$dbase = substr($dbase,0, strlen($dbase)-1);
		$basepath = $base_path;
	    $snippath = 'assets/snippets/';
	    $mngrpath = 'manager/';
	}

?>
<table border="0">
	<tr><td align="right"><nobr>Database Server:</nobr></td><td width="20">&nbsp;</td>
		<td><input type="text" name="database_server" size="30" value="<?php echo $database_server; ?>"></td></tr>
	<tr><td align="right"><nobr>Username:</nobr></td><td width="20">&nbsp;</td>
		<td><input type="text" name="database_user" size="30" value="<?php echo $database_user; ?>"></td></tr>
	<tr><td align="right"><nobr>Password:</nobr></td><td width="20">&nbsp;</td>
		<td><input type="text" name="database_password" size="30" value="<?php echo $database_password; ?>"></td></tr>
	<tr><td align="right"><nobr>Database DB:</nobr></td><td width="20">&nbsp;</td>
		<td><input type="text" name="database_db" size="30" value="<?php echo $dbase; ?>"></td></tr>
	<tr><td align="right"><nobr>Table Prefix:</nobr></td><td width="20">&nbsp;</td>
		<td><input type="text" name="table_prefix" size="30" value="<?php echo $table_prefix; ?>"></td></tr>
	
	<tr><td colspan=3>&nbsp;</td></tr>

	<tr><td align="right"><nobr>Base Path:</nobr></td><td width="20">&nbsp;</td>
		<td><input type="text" name="path_base" size="50" value="<?php echo $basepath; ?>"></td></tr>
</table><br />
	<input type="submit" value="Install Now">
	<input type="hidden" name="install" value="1">
</form>
<?php
	if ($_POST['install'] == 1 && $installok == 1) {
		$database_server = $_POST['database_server'];
	    $database_user = $_POST['database_user'];
	    $database_password = $_POST['database_password'];
	    $dbase = $_POST['database_db'];
	    $table_prefix = $_POST['table_prefix'];
	    $basepath = $_POST['path_base'];
		include_once('./install_files/installer.php');
	}
?>
        </div>
	        <div class="bar"></div>
	        <div id="footer">
	          <div id="main2">
	            <p> KiweeCommerce is licensed under the GNU <a href="http://www.gnu.org/copyleft/gpl.html" title="GPL Text" target="_blank">General Public License</a>,  and is free for both personal and business use. Commercial support is is  also available - please visit ITema's Web site via the link below and  use our Contact Us form if you're interested in our support offerings. </p>
	            <p> No warranty is made, expressed, or implied as to the proper  functionality of KiweeCommerce, this Web site, your own computer, the code  you write, or, well, anything else, for that matter. </p>
	            <p> If you find KiweeCommerce useful, please visit our <a href="http://www.kiweecommerce.org/forum.html" title="Forum">Forum</a> and drop us a line! However, please do <em>not</em> report bugs in the forum - use the <a href="http://www.kiweecommerce.org/kiwee-bugs.html" title="Bug Tracker">Bug Tracker</a>! </p>
	          </div>
	          <p align="center"><a href="http://www.itema.com/">ITema</a> - &quot;Making IT Simple&quot;</p>
	        </div>
	    </div>
	
</body>
</html>
